﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Basic4Net.Entities;

namespace Basic4Net.BusinessRules
{
    public class TeacherRelation
    {
        public bool Add(t_TeacherRelation model, ref string msg)
        {
            bool result = true;
            using (DBEntities db = new DBEntities())
            {
                //效验是否存在
                int checkName = (from a in db.t_TeacherRelation
                                 where a.TeacherID == model.TeacherID
                                 select a).Count();
                if (checkName > 0)
                {
                    result = false;
                }
                if (result)
                {
                    db.t_TeacherRelation.AddObject(model);
                    try
                    {
                        db.SaveChanges();
                    }
                    catch
                    {
                        msg = "操作失败！";
                        result = false;
                    }
                }
                else
                {
                    result = true;
                    t_TeacherRelation model2 = db.t_TeacherRelation.FirstOrDefault(t => t.TeacherID == model.TeacherID);
                    model2.TypeID = model.TypeID;
                    db.ApplyCurrentValues("t_TeacherRelation", model2);
                    try
                    {
                        db.SaveChanges();
                    }
                    catch
                    {
                        msg = "操作失败！";
                        result = false;
                    }
                }
                return result;
            }
        }
    }
}
